// 16进制转化2进制
export function hex2int(hexStr) {
    let twoStr = parseInt(hexStr,16).toString(2); // 将十六转十进制，再转2进制
    let bitNum = hexStr.length * 4; // 1个字节 = 8bit ，0xff 一个 "f"就是4位
    if(twoStr.length < bitNum){
      while(twoStr.length < bitNum){
        twoStr = "0" + twoStr;
      }
    }
    // if(twoStr.substring(0,1) == "0"){
    //   // 正数
    //   twoStr = parseInt(twoStr,2); // 二进制转十进制
    //   // console.log(hexStr + " --->>> " + twoStr)
    //   return twoStr;
    // }else{
    //   // 负数
    //   let twoStr_unsign = "";
  
    // //   console.log("hex2int--->"+parseInt(twoStr,2));
    //   twoStr = parseInt(twoStr,2) - 1; // 补码：(负数)反码+1，符号位不变；相对十进制来说也是 +1，但这里是负数，+1就是绝对值数据-1
    // //   console.log("hex2int--->"+twoStr);
  
    //   twoStr = twoStr.toString(2);
    //   twoStr_unsign = twoStr.substring(1,bitNum); // 舍弃首位(符号位)
    //   // 去除首字符，将0转为1，将1转为0   反码
    //   twoStr_unsign = twoStr_unsign.replace(/0/g, "z");
    //   twoStr_unsign = twoStr_unsign.replace(/1/g, "0");
    //   twoStr_unsign = twoStr_unsign.replace(/z/g, "1");
      
    // //   console.log("hex2int--->"+twoStr_unsign);
    // //   console.log("hex2int--->"+(-twoStr_unsign));
    //   twoStr = parseInt(-twoStr_unsign, 2);
    //   return twoStr;
    // }

    twoStr = parseInt(twoStr,2);
    return twoStr;
}

// 手环上报数据 分析
export function analyseDataSh(rawDatas, n) {
  
  let arr = groupByLenght(rawDatas, 76)

  let wristband = []

  for (var i in arr) {
    let rawData = arr[i]

    let temp={}

    temp.time = getCuTime()
    temp.time2 = getCuTimec()

    temp.online = true
    temp.mac = rawData.slice(0, 12)
    temp.step = hex2int(rawData.slice(44, 48))
    temp.calories = hex2int(rawData.slice(48, 52))
    temp.heartrate = hex2int(rawData.slice(52, 54))
    temp.systolic = hex2int(rawData.slice(54, 56))
    temp.diastolic = hex2int(rawData.slice(56, 58))

    temp.battartquantity = hex2int(rawData.slice(58, 60))
    temp.sos = hex2int(rawData.slice(60, 62))
    temp.key = hex2int(rawData.slice(62, 64))
    temp.temperature = hex2int(rawData.slice(64, 68)) / 10
    temp.sleeptime = hex2int(rawData.slice(68, 72))

    temp.bloodoxygen = hex2int(rawData.slice(72, 74))

    wristband.push(temp)
  }

  return wristband;
}
// 气体检测仪上报数据 分析
export function analyseDataGas(mac, data) {
  let gas = {}
    
  gas.mac = mac
  gas.online = true
  gas.CH4 = data.ch1[1]
  gas.O2 = data.ch2[1]
  gas.H2S = data.ch3[1]
  gas.CO = data.ch4[1]
  gas.tag = false

  gas.time = getCuTime()
  gas.time2 = getCuTimec()

  return gas;
}

export function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}
  
/**
 * 字符串按长度切割成数组
 * @param {} string 
 * @param {*} step 
 * @returns 
 */
export function groupByLenght(string,step) {
  let r = [];
  for(let i = 0, len = string.length; i < len; i+=step) {
    r.push(string.slice(i, i+step))
  }
  return r;
}

// export function hex2int(hexStr) {
//   let twoStr = parseInt(hexStr,16).toString(2); // 将十六转十进制，再转2进制
//   let bitNum = hexStr.length * 4; // 1个字节 = 8bit ，0xff 一个 "f"就是4位
//   if(twoStr.length < bitNum){
//     while(twoStr.length < bitNum){
//       twoStr = "0" + twoStr;
//     }
//   }
//   if(twoStr.substring(0,1) == "0"){
//     // 正数
//     twoStr = parseInt(twoStr,2); // 二进制转十进制
//     return twoStr;
//   }else{
//     // 负数
//     let twoStr_unsign = "";

//     console.log("hex2int--->"+parseInt(twoStr,2));
//     twoStr = parseInt(twoStr,2) - 1; // 补码：(负数)反码+1，符号位不变；相对十进制来说也是 +1，但这里是负数，+1就是绝对值数据-1
//     console.log("hex2int--->"+twoStr);

//     twoStr = twoStr.toString(2);
//     twoStr_unsign = twoStr.substring(1,bitNum); // 舍弃首位(符号位)
//     // 去除首字符，将0转为1，将1转为0   反码
//     twoStr_unsign = twoStr_unsign.replace(/0/g, "z");
//     twoStr_unsign = twoStr_unsign.replace(/1/g, "0");
//     twoStr_unsign = twoStr_unsign.replace(/z/g, "1");
    
//     console.log("hex2int--->"+twoStr_unsign);
//     console.log("hex2int--->"+(-twoStr_unsign));
//     twoStr = parseInt(-twoStr_unsign, 2);
//     return twoStr;
//   }
// }

// 时间转化
export function toHexStr() {
  var date = new Date()
  var year = date.getFullYear() // 获取年
  var month = date.getMonth() + 1 // 获取月
  var strDate = date.getDate() // 获取日
  var hour = date.getHours() // 获取小时
  var minute = date.getMinutes() // 获取分钟
  var second = date.getSeconds() // 获取秒
  return dec2hex(year, 4) + " " +  dec2hex(month, 2) + " " +  dec2hex(strDate, 2) + " " +  dec2hex(hour, 2) + " " +  dec2hex(minute, 2) + " " +  dec2hex(second, 2)
}

//10进制转16进制补0
export function dec2hex(dec, len) {//10进制转16进制补0
  var hex = "";
  while( dec ) {
    var last = dec & 15;
    hex = String.fromCharCode(((last>9)?55:48)+last) + hex;
    dec >>= 4;
  }
  if(len) {
    while(hex.length < len) hex = '0' + hex;
  }

  if(len > 2) {
    hex = insertStr(hex, 2, ' ')
  }
  if(len == 1) {
    hex = '0' + hex;
  }
  return hex;
}
// 统计位数
export function getNumLenght(dec) {
  let count = 0
  if( dec==0 ){
    count = 1
  }else{
    while(dec != 0) {
      count++;   //统计位数；
      dec = parseInt(dec/10); //赋值给 num 以备下次循环使用；
    }
  }
  return count
}

// 插入
function insertStr(source,start, newStr) {
  return source.slice(0,start) + newStr + source.slice(start)
}
 
// 获取时间
function getCuTime() {
  return new Date().toString().split(' ')[4]
}
// 获取时间
function getCuTimec() {
  return new Date().getTime()
}